﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.Reflection;
using System.Media;
using WMPLib;


namespace TramDangKiemQuangNam
{
    public partial class Form1 : Form
    {
        WMPLib.WindowsMediaPlayer Player;
        string currentDay = DateTime.Today.ToString("yyyyMMdd");
        string saveDay = System.IO.File.ReadAllText(@"Databases\saveDay.txt");
        string filePath1, filePath2;
        int currentDayNumber, saveDayNumber;
        string folderName = @"Databases";
       
        private void PlayFile(String url)
        {
            Player = new WMPLib.WindowsMediaPlayer();
            /*
            Player.PlayStateChange +=
                new WMPLib._WMPOCXEvents_PlayStateChangeEventHandler(Player_PlayStateChange);
            Player.MediaError +=
                new WMPLib._WMPOCXEvents_MediaErrorEventHandler(Player_MediaError);
            */
            Player.URL = url;
            Player.controls.play();
        }
        
        //hàm này kiểm tra xem ngày hiện tại với ngày gần nhất chương trình tắt
        private void checkedStarted()
        {
            
            currentDayNumber = Convert.ToInt32(currentDay);
            saveDayNumber = Convert.ToInt32(saveDay);
            /*
            if (currentDayNumber >= 20141026)
                Application.Exit();
            */
            filePath1 = "daychuyenso1.txt";
            filePath2 = "daychuyenso2.txt";
            
             
            folderName = System.IO.Path.Combine(folderName, currentDay);
            filePath1 = System.IO.Path.Combine(folderName, filePath1);
            filePath2 = System.IO.Path.Combine(folderName, filePath2);
            //nếu ngày lưu dữ liệu nhỏ hơn ngày bật máy thì tạo mới folder và file trong folder này
            if (saveDayNumber < currentDayNumber)
            {
                System.IO.Directory.CreateDirectory(folderName);
                using (System.IO.FileStream fs = System.IO.File.Create(filePath1)) { }
                using (System.IO.FileStream fs = System.IO.File.Create(filePath2)) { }
                System.IO.File.WriteAllText(@"Databases\saveDay.txt", currentDay);
    
            }
            else
            {
                //nếu ngày lưu dữ liệu bằng ngày mở chương trình thì kiểm tra xem
                
                //Kiểm tra folder sau đó kiểm tra file
                if(!Directory.Exists(folderName))
                {
                    //nếu chưa có folder thì tạo mới folder và hai file trong đó
                    System.IO.Directory.CreateDirectory(folderName);
                    using (System.IO.FileStream fs = System.IO.File.Create(filePath1)) { }
                    using (System.IO.FileStream fs = System.IO.File.Create(filePath2)) { }
                    System.IO.File.WriteAllText(@"Databases\saveDay.txt", currentDay);
                }
                else
                {
                    //nếu đã có folder thì kiểm tra file, nếu hai file chưa có thì tạo mới
                    if ((!File.Exists(filePath1)) && (!File.Exists(filePath2)))
                    {
                        //System.IO.Directory.CreateDirectory(folderName);
                        using (System.IO.FileStream fs = System.IO.File.Create(filePath1)) { }
                        using (System.IO.FileStream fs = System.IO.File.Create(filePath2)) { }
                        System.IO.File.WriteAllText(@"Databases\saveDay.txt", currentDay);
                    }
                    //nếu hai file này đã tồn tại thì load lên giao diện
                    else
                        loadFromDatabase(filePath1, filePath2);
                }
                
            }
        }
        private void resetCheckedlistboxItems()
        {
            checkedListBox1.Items.Clear();
            checkedListBox2.Items.Clear();
        }
        private void loadFromDatabase(string filepath1,string filepath2)
        {
            string[] daychuyen1 = new string[500];
            string[] daychuyen2 = new string[500];
            int index = 0;
            daychuyen1 = File.ReadAllLines(filepath1);
            daychuyen2 = File.ReadAllLines(filepath2);
            System.IO.File.WriteAllText(@"Databases\saveDay.txt", currentDay);
            //reset all item
            //resetCheckedlistboxItems();
            foreach(string line in daychuyen1)
            {
                if(line!="")
                    checkedListBox1.Items.Insert(index++, line);
            }
            index = 0;
            foreach (string line in daychuyen2)
            {
                if(line!="")
                    checkedListBox2.Items.Insert(index++, line);
            }

        }
        private void saveToDatabase(string filepath1,string filepath2)
        {
            string[] daychuyen1 = new string[500];
            string[] daychuyen2 = new string[500];
            //luu day chuyen so 1
            int index = 0;
            foreach(object item in checkedListBox1.Items)
            {
                daychuyen1[index++] = item.ToString();
            }
            System.IO.File.WriteAllLines(filepath1, daychuyen1);
            index = 0;
            //luu day chuyen so 2
            foreach (object item in checkedListBox2.Items)
            {
                daychuyen2[index++] = item.ToString();
            }
            System.IO.File.WriteAllLines(filepath2, daychuyen2);
        }
        public Form1()
        {
            InitializeComponent();
            System.Windows.Forms.ToolTip ToolTip1 = new System.Windows.Forms.ToolTip();
            ToolTip1.SetToolTip(this.button3, "Đưa xe vào dây chuyền để kiểm tra lần 1");
            ToolTip1.SetToolTip(this.button4, "Đưa xe vào dây chuyền để kiểm tra lần 2");
            ToolTip1.SetToolTip(this.button5, "Mời chủ phương tiện về bàn kết luận để nghe hướng dẫn");
            ToolTip1.SetToolTip(this.button6, "Mời chủ phương tiện đưa xe về đầu cuối dây chuyền để chụp hình");
            ToolTip1.SetToolTip(this.button7, "Mời chủ phương tiện đưa xe ra kiểm tra khí xả");
            ToolTip1.SetToolTip(this.button13, "Mời chủ phương tiện gỡ tem cũ nộp về bàn kết luận");
            ToolTip1.SetToolTip(this.button15, "Mời chủ phương tiện về phòng hồ sơ có việc cần");
            ToolTip1.SetToolTip(this.button9, "Mời chủ phương tiện về phòng hồ sơ để nộp phí sử dụng đường bộ");
            /*Login lg = new Login();
            lg.ShowDialog();*/
            //kiem tra ngay thang
            
        }
        
        public string change_text(string s)
        {
            int p = s.Length;
            if(p==9)
            {
                s = s.Insert(4, "-");
            }
            else
            {
                s = s.Insert(3, "-");
                if (p == 8)
                    s = s.Insert(7, ".");
            }
            return s;
        }
         
        private void label1_Click(object sender, EventArgs e)
        {

        }
        //them ghi chu
        private void ghichu(ColorCodedCheckedListBox chklb, string a="*")
        {
            string tam;
            foreach (int checkedItemIndex in chklb.CheckedIndices)
            {
                tam = a + chklb.Items[checkedItemIndex].ToString() + a;
                chklb.Items.RemoveAt(checkedItemIndex);
                chklb.Items.Insert(checkedItemIndex, tam);
                //8/3/2014
                chklb.SetItemCheckState(checkedItemIndex, CheckState.Indeterminate);
                //
            }

        }
        //10/3
        private void PlaylistFinal(string malenh, int a = 0, int delay = 0)
        {
            WMPLib.IWMPPlaylist playlist = axWindowsMediaPlayer2.playlistCollection.newPlaylist("myplaylist");
            WMPLib.IWMPMedia media;
            WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer();
            //10/3
            axWindowsMediaPlayer2.Ctlcontrols.stop();
            ColorCodedCheckedListBox chklb;
            //
            string path = "", fullpath = "";
            int dem = 0;
            int N1 = 0, N2 = 0;
            //double T1 = 0, T2 = 0;
            //int checked1 = -1, checked2 = -1;day
            resetbutton();
            //button8.PerformClick();
            //checkedListBox1.SelectionMode = SelectionMode.None;
            //checkedListBox2.SelectionMode = SelectionMode.None;
            axWindowsMediaPlayer2.Ctlcontrols.stop();
            textBox1.Text = "";
            path = "[";
            fullpath += path;
            // kiem tra checklisbox1
            timer1.Enabled = true;
            timer2.Enabled = true;

            //Chạy dây chuyền thứ nhất
            chklb = checkedListBox1;
            if (a != 0)
            {
                ghichu(chklb);
            }
            foreach (object itemchecked in chklb.CheckedItems)
            {

                fullpath += itemchecked.ToString() + "W";
                dem++;
            }

            if (dem != 1)
            {
                N1 = 20000 + (dem - 1) * 11000;
            }
            //N1 = timer2.Interval;
            //else timer2.Interval = 17500;
            //textBox1.Text = timer2.Interval.ToString();
            
            //disablingitem(checkedListBox1);
            fullpath = fullpath.Substring(0, fullpath.Length - 1);
            fullpath = fullpath.Replace("-", "");
            fullpath = fullpath.Replace(".", "");
            fullpath = fullpath.Replace("*", "");
            fullpath = fullpath.Replace("$", "");
            /*
            //smoothing
            fullpath = fullpath.Replace("92", "Ê");
            fullpath = fullpath.Replace("43", "{");
            fullpath = fullpath.Replace("75", "Â");
            fullpath = fullpath.Replace("76", "}");
            //end smoothing
             * */
            //doc mot lan
            if (checkBox1.Checked == false)
            {
                fullpath = fullpath + "]";
                fullpath += fullpath;
                fullpath = fullpath.Substring(0, fullpath.Length - 1);
            }
            else
            {
                //timer2.Interval = timer2.Interval / 2;
                N1 = N1 / 2;
            }
            
            //textBox1.Text = fullpath;

            if (fullpath.Length > 3)
            {
                for (int i = 0; i < fullpath.Length; i++)
                {
                    //path = System.IO.Directory.GetCurrentDirectory().ToString() + @"\Sounds\" + fullpath[i] + ".mp3";
                    //sua lai doan doc ma lenh
                    if (fullpath[i] == ']')
                    {
                        path = @"Sounds\" + malenh + ".mp3";
                        media = axWindowsMediaPlayer2.newMedia(path);
                        playlist.appendItem(media);
                    }
                    //het doan
                    path = @"Sounds\" + fullpath[i] + ".mp3";
                    media = axWindowsMediaPlayer2.newMedia(path);
                    playlist.appendItem(media);
                    //axWindowsMediaPlayer2.currentPlaylist = playlist;
                    //axWindowsMediaPlayer2.Ctlcontrols.play();
                }
                //path = System.IO.Directory.GetCurrentDirectory().ToString() + @"\Sounds\" + malenh + ".mp3";
                path = @"Sounds\" + malenh + ".mp3";
                media = axWindowsMediaPlayer2.newMedia(path);
                playlist.appendItem(media);
                //path = System.IO.Directory.GetCurrentDirectory().ToString() + @"\Sounds\-.mp3";
                path = @"Sounds\--.mp3";
                media = axWindowsMediaPlayer2.newMedia(path);
                playlist.appendItem(media);
                //thu nghiem do dai playlist
                //T1=wplayer.currentMedia.duration;
                //axWindowsMediaPlayer2.currentPlaylist = playlist;
                //axWindowsMediaPlayer2.Ctlcontrols.play();
                //
            //chạy dây chuyền thứ hai
            axWindowsMediaPlayer2.Ctlcontrols.stop();
            textBox1.Text = "";
            path = "[";
            fullpath = "";
            fullpath += path;
            chklb = checkedListBox2;
            malenh += "2";
            dem = 0;
            foreach (object itemchecked in chklb.CheckedItems)
            {

                fullpath += itemchecked.ToString() + "W";
                dem++;
            }

            if (dem != 1)
            {
                //timer2.Interval += 22000 + (dem - 1) * 9000;
                N2 = 20000 + (dem - 1) * 11000;
            }
           
            //else timer2.Interval = 17500;
            //textBox1.Text = timer2.Interval.ToString();

            //disablingitem(checkedListBox1);
            if (a != 0)
            {
                ghichu(chklb);
            }
            fullpath = fullpath.Substring(0, fullpath.Length - 1);
            fullpath = fullpath.Replace("-", "");
            fullpath = fullpath.Replace(".", "");
            fullpath = fullpath.Replace("*", "");
            fullpath = fullpath.Replace("$", "");
                /*
            //smoothing
            fullpath = fullpath.Replace("92", "Ê");
            fullpath = fullpath.Replace("43", "{");
            fullpath = fullpath.Replace("75", "Â");
            fullpath = fullpath.Replace("76", "}");
            //end smoothing
                 * */
            if (checkBox2.Checked == false)
            {
                fullpath = fullpath + "]";
                fullpath += fullpath;
                fullpath = fullpath.Substring(0, fullpath.Length - 1);
            }
            else
            {
                //timer2.Interval = timer2.Interval / 2;
                N2 = N2 / 2;
            }
            timer2.Interval = N1 + N2 + 10000;
            if (fullpath.Length > 3)
            {
                for (int i = 0; i < fullpath.Length; i++)
                {
                    //path = System.IO.Directory.GetCurrentDirectory().ToString() + @"\Sounds\" + fullpath[i] + ".mp3";
                    //sua lai doan doc ma lenh
                    if (fullpath[i] == ']')
                    {
                        path = @"Sounds\" + malenh + ".mp3";
                        media = axWindowsMediaPlayer2.newMedia(path);
                        playlist.appendItem(media);
                    }
                    //het doan
                    path = @"Sounds\" + fullpath[i] + ".mp3";
                    media = axWindowsMediaPlayer2.newMedia(path);
                    playlist.appendItem(media);
                    //axWindowsMediaPlayer2.currentPlaylist = playlist;
                    //axWindowsMediaPlayer2.Ctlcontrols.play();
                }
                //path = System.IO.Directory.GetCurrentDirectory().ToString() + @"\Sounds\" + malenh + ".mp3";
                path = @"Sounds\" + malenh + ".mp3";
                media = axWindowsMediaPlayer2.newMedia(path);
                playlist.appendItem(media);
                //path = System.IO.Directory.GetCurrentDirectory().ToString() + @"\Sounds\-.mp3";
                path = @"Sounds\-.mp3";
                media = axWindowsMediaPlayer2.newMedia(path);
                playlist.appendItem(media);
                axWindowsMediaPlayer2.currentPlaylist = playlist;
                //axWindowsMediaPlayer2.Ctlcontrols.play();
                //
                //T2 = wplayer.currentMedia.duration;
                //timer2.Interval = (int)(T1 + T2);
            }

          }
        }
        //10/3
        private void checkedPlaylist(string malenh,int a=0,string gchu="")
        {
            int checked1 = -1, checked2 = -1;
            ColorCodedCheckedListBox chklb;
            foreach (int checkedItemIndex in checkedListBox1.CheckedIndices)
            {
                checked1 = checkedItemIndex;
                //8/3/2014
                checkedListBox1.SetItemCheckState(checkedItemIndex, CheckState.Indeterminate);
                //checkedListBox1.SetSelected(checkedItemIndex, false);
                //
            }
            //kiem tra checkedlistbox2
            foreach (int checkedItemIndex in checkedListBox2.CheckedIndices)
            {
                checked2 = checkedItemIndex;
                //8/3
                checkedListBox2.SetItemCheckState(checkedItemIndex, CheckState.Indeterminate);
                //checkedListBox2.SetSelected(checkedItemIndex, false);
                //
            }
            if((checked1!=-1) && (checked2!=-1))
            {
                PlaylistFinal(malenh);
                ghichu(checkedListBox2, gchu);
                ghichu(checkedListBox1, gchu);
                //PlaylistFinal(checkedListBox2, malenh);
             
            }
            else
            {
                if (checked1 != -1) chklb = checkedListBox1;
                else chklb = checkedListBox2;
                ghichu(chklb, gchu);
                CreatePlayLis(malenh);
            }
        }
       
        //override createplaylis
        private void CreatePlayLis(string malenh,int a=0,int delay=0)
        {
            WMPLib.IWMPPlaylist playlist = axWindowsMediaPlayer2.playlistCollection.newPlaylist("myplaylist");
            WMPLib.IWMPMedia media;
            WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer();
            //10/3
            axWindowsMediaPlayer2.Ctlcontrols.stop();
            //
            string path = "", fullpath = "";
            int checked1 = -1, checked2 = -1;
            int dem = 0;
            ColorCodedCheckedListBox chklb;
            int selected = -1;
            //bool ischecked1=false, ischecked2=false;
            resetbutton();
            //button8.PerformClick();
            //checkedListBox1.SelectionMode = SelectionMode.None;
            //checkedListBox2.SelectionMode = SelectionMode.None;
            axWindowsMediaPlayer2.Ctlcontrols.stop();
            //8/3/2014
            //timer2.Enabled = true;
            //textBox1.Text = saveDay;
            path = "[";
            fullpath += path;
            // kiem tra checklisbox1
            foreach (int checkedItemIndex in checkedListBox1.CheckedIndices)
            {
                checked1 = checkedItemIndex;
                //8/3/2014
                checkedListBox1.SetItemCheckState(checkedItemIndex, CheckState.Indeterminate);
                //checkedListBox1.SetSelected(checkedItemIndex, false);
                //
            }
            //kiem tra checkedlistbox2
            foreach (int checkedItemIndex in checkedListBox2.CheckedIndices)
            {
                checked2 = checkedItemIndex;
                //8/3
                checkedListBox2.SetItemCheckState(checkedItemIndex, CheckState.Indeterminate);
                //checkedListBox2.SetSelected(checkedItemIndex, false);
                //
            }
            /*
            if((checked1!=-1) && (checked2!=-1))
            {
                MessageBox.Show("Không thể chọn cùng lúc xe trong cả hai dây chuyền!", "Thông báo!", MessageBoxButtons.OK);
                uncheckitem(checkedListBox1);
                uncheckitem(checkedListBox2);
                button8.PerformClick();
            }
            else
            {
            }*/

            if ((checked1 == -1) && (checked2 == -1))
            {
                timer1.Enabled = false;
            }
            else
            {
                timer1.Enabled = true;
                timer2.Enabled = true;
                if (checked1 > -1)
                {
                    chklb = checkedListBox1;
                    //ischecked1 = true;
                    selected = 1;
                }
                else
                {
                    chklb = checkedListBox2;
                    malenh += "2";
                    //ischecked2 = true;
                    selected = 2;
                }

                foreach (object itemchecked in chklb.CheckedItems)
                {

                    fullpath += itemchecked.ToString() + "W";
                    dem++;
                }
               
                if (dem != 1)
                {
                    timer2.Interval = 20000 + (dem - 1) * 11000;
                }

                //textBox1.Text = timer2.Interval.ToString();
                if (a != 0)
                {
                    ghichu(chklb);
                }
                fullpath = fullpath.Substring(0, fullpath.Length - 1);
                fullpath = fullpath.Replace("-", "");
                fullpath = fullpath.Replace(".", "");
                fullpath = fullpath.Replace("*", "");
                fullpath = fullpath.Replace("$", "");
                /*
                //smoothing
                fullpath = fullpath.Replace("92", "Ê");
                fullpath = fullpath.Replace("43", "{");
                fullpath = fullpath.Replace("75", "Â");
                fullpath = fullpath.Replace("76", "}");
                //end smoothing
                 * */
                if (((selected == 1) && (checkBox1.Checked == false)) || ((selected == 2) && (checkBox2.Checked == false)))
                {
                    fullpath = fullpath + "]";
                    fullpath += fullpath;
                    fullpath = fullpath.Substring(0, fullpath.Length - 1);
                }
                else
                {
                    timer2.Interval = timer2.Interval / 2;
                }

                //textBox1.Text = fullpath;
                //timer2.Interval = 17000;
                if (fullpath.Length > 3)
                {
                    for (int i = 0; i < fullpath.Length; i++)
                    {
                        //path = System.IO.Directory.GetCurrentDirectory().ToString() + @"\Sounds\" + fullpath[i] + ".mp3";
                        //sua lai doan doc ma lenh
                        if (fullpath[i] == ']')
                        {
                            path = @"Sounds\" + malenh + ".mp3";
                            media = axWindowsMediaPlayer2.newMedia(path);
                            playlist.appendItem(media);
                        }
                        //het doan
                        path = @"Sounds\" + fullpath[i] + ".mp3";
                        media = axWindowsMediaPlayer2.newMedia(path);
                        playlist.appendItem(media);
                        //axWindowsMediaPlayer2.currentPlaylist = playlist;
                        //axWindowsMediaPlayer2.Ctlcontrols.play();
                    }
                    //path = System.IO.Directory.GetCurrentDirectory().ToString() + @"\Sounds\" + malenh + ".mp3";
                    path = @"Sounds\" + malenh + ".mp3";
                    media = axWindowsMediaPlayer2.newMedia(path);
                    playlist.appendItem(media);
                    //path = System.IO.Directory.GetCurrentDirectory().ToString() + @"\Sounds\-.mp3";
                    path = @"Sounds\-.mp3";
                    media = axWindowsMediaPlayer2.newMedia(path);
                    playlist.appendItem(media);
                    axWindowsMediaPlayer2.currentPlaylist = playlist;
                    //axWindowsMediaPlayer2.Ctlcontrols.play();
                }
                //uncheckitem(chklb);//bỏ đi ngày 8/3/2014
            }
        }
        //playlist 2
        private void CreatePlayLis2(string malenh)
        {
            WMPLib.IWMPPlaylist playlist = axWindowsMediaPlayer2.playlistCollection.newPlaylist("myplaylist");
            WMPLib.IWMPMedia media;
            WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer();
            //10/3
            axWindowsMediaPlayer2.Ctlcontrols.stop();
            //
            string path = "", fullpath = "";
            int dem = 0;
            int checked1 = -1, checked2 = -1;
            resetbutton();
            //button8.PerformClick();
            //checkedListBox1.SelectionMode = SelectionMode.None;
            //checkedListBox2.SelectionMode = SelectionMode.None;
            axWindowsMediaPlayer2.Ctlcontrols.stop();
            //8/3/2014
            foreach (int checkedItemIndex in checkedListBox1.CheckedIndices)
            {
                checked1 = checkedItemIndex;
            }
            //kiem tra checkedlistbox2
            foreach (int checkedItemIndex in checkedListBox2.CheckedIndices)
            {
                checked2 = checkedItemIndex;
            }
            if((checked1!=-1) || (checked2!=-1))
            {
                textBox1.Text = "";
                path = "[";
                fullpath += path;
                // kiem tra checklisbox1
                timer1.Enabled = true;
                timer2.Enabled = true;
                foreach (object itemchecked in checkedListBox1.CheckedItems)
                {

                    fullpath += itemchecked.ToString() + "W";
                    dem++;
                }

                foreach (object itemchecked in checkedListBox2.CheckedItems)
                {
                    fullpath += itemchecked.ToString() + "W";
                    dem++;
                }
                if (dem != 1)
                {
                    timer2.Interval = 20000 + (dem - 1) * 11000;
                }
                //else timer2.Interval = 17500;
                //textBox1.Text = timer2.Interval.ToString();
                //disablingitem(checkedListBox1);
                fullpath = fullpath.Substring(0, fullpath.Length - 1);
                fullpath = fullpath.Replace("-", "");
                fullpath = fullpath.Replace(".", "");
                fullpath = fullpath.Replace("*", "");
                fullpath = fullpath.Replace("$", "");
                /*
                //smoothing
                fullpath = fullpath.Replace("92", "Ê");
                fullpath = fullpath.Replace("43", "{");
                fullpath = fullpath.Replace("75", "Â");
                fullpath = fullpath.Replace("76", "}");
                //end smoothing
                 */
                if ((checkBox1.Checked == false) && (checkBox2.Checked==false))
                {
                    fullpath = fullpath + "]";
                    fullpath += fullpath;
                    fullpath = fullpath.Substring(0, fullpath.Length - 1);
                }
                else
                {
                    timer2.Interval = timer2.Interval / 2;
                }
                if (fullpath.Length > 3)
                {
                    for (int i = 0; i < fullpath.Length; i++)
                    {
                        //path = System.IO.Directory.GetCurrentDirectory().ToString() + @"\Sounds\" + fullpath[i] + ".mp3";
                        //sua lai doan doc ma lenh
                        if (fullpath[i] == ']')
                        {
                            path = @"Sounds\" + malenh + ".mp3";
                            media = axWindowsMediaPlayer2.newMedia(path);
                            playlist.appendItem(media);
                        }
                        //het doan
                        path = @"Sounds\" + fullpath[i] + ".mp3";
                        media = axWindowsMediaPlayer2.newMedia(path);
                        playlist.appendItem(media);
                        //axWindowsMediaPlayer2.currentPlaylist = playlist;
                        //axWindowsMediaPlayer2.Ctlcontrols.play();
                    }
                    //path = System.IO.Directory.GetCurrentDirectory().ToString() + @"\Sounds\" + malenh + ".mp3";
                    path = @"Sounds\" + malenh + ".mp3";
                    media = axWindowsMediaPlayer2.newMedia(path);
                    playlist.appendItem(media);
                    //path = System.IO.Directory.GetCurrentDirectory().ToString() + @"\Sounds\-.mp3";
                    path = @"Sounds\-.mp3";
                    media = axWindowsMediaPlayer2.newMedia(path);
                    playlist.appendItem(media);
                    axWindowsMediaPlayer2.currentPlaylist = playlist;
                    //axWindowsMediaPlayer2.Ctlcontrols.play();
                }
            }
            
        }
        //remove selected item
        private void removeitem()
        {
            /*
            foreach (int checkedItemIndex1 in checkedListBox1.CheckedIndices)
            {
                checkedListBox1.Items.RemoveAt(checkedItemIndex1);
            }
            foreach (int checkedItemIndex2 in checkedListBox2.CheckedIndices)
            {
                checkedListBox2.Items.RemoveAt(checkedItemIndex2);
            }
            */
            //9/3/2014
            /*
            while (checkedListBox1.CheckedItems.Count > 0)
            {
                checkedListBox1.Items.Remove(checkedListBox1.CheckedItems[0]);
            }
            while (checkedListBox2.CheckedItems.Count > 0)
            {
                checkedListBox1.Items.Remove(checkedListBox2.CheckedItems[0]);
            }*/
            //
            //10/3
            CheckedListBox.CheckedItemCollection checkedItemCollection = checkedListBox1.CheckedItems;

            for (int i = checkedItemCollection.Count; i > 0; i--)
            {

                checkedListBox1.Items.Remove(checkedItemCollection[i - 1]);

            }
            CheckedListBox.CheckedItemCollection checkedItemCollection2 = checkedListBox2.CheckedItems;

            for (int i = checkedItemCollection2.Count; i > 0; i--)
            {

                checkedListBox2.Items.Remove(checkedItemCollection2[i - 1]);

            }
            //
        }
        //unchecked item selected
        private void uncheckitem(ColorCodedCheckedListBox chklb)
        {
            foreach (int checkedItemIndex in chklb.CheckedIndices)
            {
                chklb.SetItemChecked(checkedItemIndex, false);
            }
        }
        //disabling item
        private void disablingitem(ColorCodedCheckedListBox chklb)
        {
            foreach (int checkedItemIndex in chklb.CheckedIndices)
            {
                //chklb.SetItemChecked(checkedItemIndex, false);
                chklb.SetItemCheckState(checkedItemIndex, CheckState.Indeterminate);
                
                //checkedListBox1.SetItemCheckState(0, CheckState.Indeterminate);
                
            }
        }
        //reset button
        private void resetbutton()
        {
            Button btn = new Button();
            Color goc=btn.BackColor;
            foreach (Control c in this.Controls)    
            {
                c.BackColor = goc;
            } 

        }
        private void button3_Click(object sender, EventArgs e)
        {
            string ml = "lenh1", gchu = "*";
            int gc = 1;
            checkedPlaylist(ml, gc,gchu);
            //CreatePlayLis(ml,gc);
            button3.BackColor = Color.GreenYellow;
            //uncheckitem(checkedListBox1);
            //dem++;
            //CreatePlayLis(ml, dem);

        }

        private void button13_Click(object sender, EventArgs e)
        {
            string ml = "lenhchung1";
            CreatePlayLis2(ml);
            ghichu(checkedListBox2, "$");
            ghichu(checkedListBox1, "$");
            button13.BackColor = Color.GreenYellow;
            disablingitem(checkedListBox1);
            disablingitem(checkedListBox2);
        }

        private void button15_Click(object sender, EventArgs e)
        {
            string ml = "lenhchung2";
            CreatePlayLis2(ml);
            //ghichu(checkedListBox2, "$");
            //ghichu(checkedListBox1, "$");
            button15.BackColor = Color.GreenYellow;
            disablingitem(checkedListBox1);
            disablingitem(checkedListBox2);
            
        }

        private void button9_Click(object sender, EventArgs e)
        {
            string ml = "lenhchung3";
            CreatePlayLis2(ml);
            //ghichu(checkedListBox2, "$");
            //ghichu(checkedListBox1, "$");
            button9.BackColor = Color.GreenYellow;
            disablingitem(checkedListBox1);
            disablingitem(checkedListBox2);
        }
        private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        //override phim dong chuong trinh
        /*
        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            
            base.OnFormClosing(e);

            if (e.CloseReason == CloseReason.WindowsShutDown) return;
       
            // Confirm user wants to close
            switch (MessageBox.Show(this, "Bạn có muốn thoát chương trình và lưu CSDL?", "Xác nhận!", MessageBoxButtons.YesNo))
            {
                case DialogResult.No:
                    e.Cancel = true;
                    
                    break;
                default:
                    //Application.Exit();
                    //this.Close();
                    break;
            }
        }*/
        //10/3
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            const string message =
                "Bạn có muốn đóng chương trình và lưu Cơ sở Dữ liệu?";
            const string caption = "Xác nhận!";
            var result = MessageBox.Show(message, caption,
                                         MessageBoxButtons.OKCancel,
                                         MessageBoxIcon.Question);

            // If the no button was pressed ... 
            if (result == DialogResult.Cancel)
            {
                // cancel the closure of the form.
                e.Cancel = true;
            }
            if (result == DialogResult.OK)
            {
                saveToDatabase(filePath1, filePath2);
                //axWindowsMediaPlayer2.Ctlcontrols.stop();
                //button8.PerformClick();
                //this.Close();
                // cancel the closure of the form.
                //Application.Exit();
                //this.Close();
            }
        }
        //
        private void button4_Click(object sender, EventArgs e)
        {
            string ml = "lenh2";
            checkedPlaylist(ml);
            //CreatePlayLis(ml);
            button4.BackColor = Color.GreenYellow;
            //uncheckitem(checkedListBox1);
            //dem++;
            //CreatePlayLis(ml, dem);
            //chế thêm ngày 5/3/2014
            
            //
        }

        private void button5_Click(object sender, EventArgs e)
        {
            string ml = "lenh3";
            checkedPlaylist(ml);
            //CreatePlayLis(ml);
            button5.BackColor = Color.GreenYellow;
            //uncheckitem(checkedListBox1);
            //dem++;
            //CreatePlayLis(ml, dem);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            string ml = "lenh4";
            checkedPlaylist(ml);
            //CreatePlayLis(ml);
            button6.BackColor = Color.GreenYellow;
            //uncheckitem(checkedListBox1);
            //dem++;
            //CreatePlayLis(ml, dem);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            string ml = "lenh5";
            //10/3
            checkedPlaylist(ml);
            //CreatePlayLis(ml);
            button7.BackColor = Color.GreenYellow;
            //uncheckitem(checkedListBox1);
            //dem++;
            //CreatePlayLis(ml, dem);
        }
        private void axWindowsMediaPlayer2_Enter(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if(textBox1.Text.Length > 9 || textBox1.Text.Length < 7)
            {
                MessageBox.Show("Nhập Biển số xe sai quy định!", "Thông báo!", MessageBoxButtons.OK);
            }
            else
            {
                textBox1.Text = change_text(textBox1.Text);
                checkedListBox1.Items.Add(textBox1.Text);
                textBox1.Text = "";
            }
        }

        private void button14_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Length > 9 || textBox1.Text.Length < 7)
            {
                MessageBox.Show("Nhập Biển số xe sai quy định!", "Thông báo!", MessageBoxButtons.OK);
            }
            else
            {
                textBox1.Text = change_text(textBox1.Text);
                checkedListBox2.Items.Add(textBox1.Text);
                textBox1.Text = "";
            }
        }

 
        private void setWindowsAlwaysOnTopToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //uncheckitem(checkedListBox1);
            //uncheckitem(checkedListBox2);
            
        }

        private void setWindowsNomalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.TopMost = false;
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            About ab = new About();
            ab.ShowDialog();
        }

        private void textBox1_TextChanged(object sender, EventArgs e, KeyEventArgs k)
        {
            if (k.KeyCode == Keys.Enter)
            {
                //button2.Click(sender, e);
            }
        }

        private void exitToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void aboutToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            About ab = new About();
            ab.ShowDialog();
        }

        private void setWindowsAlwaysOnTopToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            this.TopMost = true;
        }

        private void setWindowsNomalToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            this.TopMost = false;
        }

        private void checkedListBox1_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            resetbutton();
            button1.BackColor = Color.GreenYellow;
            removeitem();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            //textBox1_KeyDown()
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode == Keys.Enter) || (e.KeyCode==Keys.Left))
            {
                button2.PerformClick();
            }
            if(e.KeyCode==Keys.Right)
            {
                button14.PerformClick();
            }
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            //if (e.KeyChar == (char)Keys.Left)
            //{
            //    MessageBox.Show("Nhap vao luong 1");
            //}
        }

        private void checkedListBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            
        }

        private void checkedListBox1_KeyDown(object sender, KeyEventArgs e)
        {
            //string vitri;
            //if(e.KeyCode==Keys.Enter)
            //{
            //    vitri = checkedListBox1.SelectedItem.ToString();
            //}
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                axWindowsMediaPlayer2.Ctlcontrols.play();
            }
            catch (Exception ex)
            {
                //Console.WriteLine("An error occurred: '{0}'", e);
                //MessageBox.Show("Thông báo", "Lỗi xãy ra ở đây!", MessageBoxButtons.OK);
            }
            //button8.Focus();
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            //timer1.Enabled = false;
            //timer2.Enabled = false;
            button8.PerformClick();
            //timer1.Enabled = false;
            //đoạn code chế ngày 5/3/2014
            uncheckitem(checkedListBox1);
            uncheckitem(checkedListBox2);
            //hết đoạn
            
        }

        private void button8_Click(object sender, EventArgs e)
        {
            resetbutton();
            //button8.BackColor = Color.GreenYellow;
            resettimer();
            axWindowsMediaPlayer2.Ctlcontrols.stop();
            //thêm vào ngày 8/3/2014
            uncheckitem(checkedListBox1);
            uncheckitem(checkedListBox2);
            //
            
        }
        private void resettimer()
        {
            timer1.Interval = 10;
            timer1.Enabled = false;
            timer2.Interval = 26500;
            timer2.Enabled = false;
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            
        }

        private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void thôngTinPhầnMềToolStripMenuItem_Click(object sender, EventArgs e)
        {
            About ab = new About();
            ab.ShowDialog();
        }

        private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
        {

        }

        private void pictureBox1_Click_1(object sender, EventArgs e)
        {

        }

        private void timer3_Tick(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            checkedStarted();

        }

        private void lưuVàoCSDLToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveToDatabase(filePath1, filePath2);
        }

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void tảiCơSởDữLiệuToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string fileCsdl1 = "daychuyenso1.txt";
            string fileCsdl2 = "daychuyenso2.txt";
            bool is64bit = !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"));
            using (FolderBrowserDialog dlg = new FolderBrowserDialog())
            {
                if(is64bit==true)
                    dlg.SelectedPath = @"C:\Program Files (x86)\Trungdinh Software\QuangNam Register Final\Databases";
                else
                    dlg.SelectedPath = @"C:\Program Files\Trungdinh Software\QuangNam Register Final\Databases";
                dlg.Description = "Chọn Folder lưu Cơ sở Dữ liệu";
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    fileCsdl1 = System.IO.Path.Combine(dlg.SelectedPath, fileCsdl1);
                    fileCsdl2 = System.IO.Path.Combine(dlg.SelectedPath, fileCsdl2);
                    //MessageBox.Show("You selected: " + fileCsdl1);
                    resetCheckedlistboxItems();
                    loadFromDatabase(fileCsdl1, fileCsdl2);
                }
            }
        }

        private void button10_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < checkedListBox1.Items.Count; i++)

                checkedListBox1.SetItemChecked(i, true);
        }

        private void button12_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < checkedListBox2.Items.Count; i++)

                checkedListBox2.SetItemChecked(i, true);
        }

        private void button11_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < checkedListBox1.Items.Count; i++)

                checkedListBox1.SetItemChecked(i, false);
        }

        private void button16_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < checkedListBox2.Items.Count; i++)

                checkedListBox2.SetItemChecked(i, false);
        }

    }
    class ColorCodedCheckedListBox : CheckedListBox
    {
        public Color UncheckedColor { get; set; }
        public Color CheckedColor { get; set; }
        public Color IndeterminateColor { get; set; }
        public ColorCodedCheckedListBox()
        {
            UncheckedColor = Color.Green;
            CheckedColor = Color.Red;
            IndeterminateColor = Color.Orange;
        }
        public ColorCodedCheckedListBox(Color uncheckedColor, Color checkedColor, Color indeterminateColor)
        {
            UncheckedColor = uncheckedColor;
            CheckedColor = checkedColor;
            IndeterminateColor = indeterminateColor;
        }
        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            if (this.DesignMode)
            {
                base.OnDrawItem(e);
            }
            else
            {
                Color textColor = this.GetItemCheckState(e.Index) == CheckState.Unchecked ? UncheckedColor : (this.GetItemCheckState(e.Index) == CheckState.Checked ? CheckedColor : IndeterminateColor);

                DrawItemEventArgs e2 = new DrawItemEventArgs
                   (e.Graphics,
                    e.Font,
                    new Rectangle(e.Bounds.Location, e.Bounds.Size),
                    e.Index,
                    (e.State & DrawItemState.Focus) == DrawItemState.Focus ? DrawItemState.Focus : DrawItemState.None, /* Remove 'selected' state so that the base.OnDrawItem doesn't obliterate the work we are doing here. */
                    textColor,
                    this.BackColor);

                base.OnDrawItem(e2);
            }
        }
    }
    class ColouredCheckedListBox : CheckedListBox
    {
        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            DrawItemEventArgs e2 =
                new DrawItemEventArgs
                (
                    e.Graphics,
                    e.Font,
                    new Rectangle(e.Bounds.Location, e.Bounds.Size),
                    e.Index,
                    e.State,
                    e.ForeColor,
                    this.CheckedIndices.Contains(e.Index) ? Color.Yellow : SystemColors.Window
                );

            base.OnDrawItem(e2);
        }
    }
    /*public class CustomCheckedListBox : CheckedListBox
    {
        public CustomCheckedListBox()
        {
            DoubleBuffered = true;
        }
        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            Size checkSize = CheckBoxRenderer.GetGlyphSize(e.Graphics, System.Windows.Forms.VisualStyles.CheckBoxState.MixedNormal);
            int dx = (e.Bounds.Height - checkSize.Width) / 2;
            e.DrawBackground();
            bool isChecked = GetItemChecked(e.Index);//For some reason e.State doesn't work so we have to do this instead.
            CheckBoxRenderer.DrawCheckBox(e.Graphics, new Point(dx, e.Bounds.Top + dx), isChecked ? System.Windows.Forms.VisualStyles.CheckBoxState.CheckedNormal : System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal);
            using (StringFormat sf = new StringFormat { LineAlignment = StringAlignment.Center })
            {
                using (Brush brush = new SolidBrush(isChecked ? CheckedItemColor : ForeColor))
                {
                    e.Graphics.DrawString(Items[e.Index].ToString(), Font, brush, new Rectangle(e.Bounds.Height, e.Bounds.Top, e.Bounds.Width - e.Bounds.Height, e.Bounds.Height), sf);
                }
            }
        }
        Color checkedItemColor = Color.Green;
        public Color CheckedItemColor
        {
            get { return checkedItemColor; }
            set
            {
                checkedItemColor = value;
                Invalidate();
            }
        }
    }*/
}
/*
doan lenh phat am thanh
WMPLib.IWMPPlaylist playlist = axWindowsMediaPlayer2.playlistCollection.newPlaylist("myplaylist");
            WMPLib.IWMPMedia media;
            WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer();
            string[] duongdan = new string[500];
            string fullpath;
            int i=0;
           
            /*for (int i = 0; i < checkedListBox1.Items.Count; i++)
            {
                duongdan[i] = checkedListBox1.CheckedItems[i].ToString();
                string tam = duongdan[i];
                for (int dem = 0; dem < 5; dem++)
                {
                    fullpath = System.IO.Directory.GetCurrentDirectory().ToString() + @"\Sounds\" + tam[dem] + ".mp3";
                    media = axWindowsMediaPlayer2.newMedia(fullpath);
                    playlist.appendItem(media);
                    fullpath = System.IO.Directory.GetCurrentDirectory().ToString() + @"\Sounds\lenh3.mp3";
                    media = axWindowsMediaPlayer2.newMedia(fullpath);
                    playlist.appendItem(media);
                    axWindowsMediaPlayer2.currentPlaylist = playlist;
                    axWindowsMediaPlayer2.Ctlcontrols.play();
                    axWindowsMediaPlayer2.playlistCollection.remove(playlist);
                }
            }
            foreach(object itemchecked in checkedListBox1.CheckedItems)
            {
                duongdan[i] = itemchecked.ToString();
                i++;
            }
            for(int j=0;j<i;j++)
            {
                string tam = duongdan[j];
                for (int dem = 0; dem < tam.Length; dem++)
                {
                    fullpath = System.IO.Directory.GetCurrentDirectory().ToString() + @"\Sounds\" + tam[dem] + ".mp3";
                    media = axWindowsMediaPlayer2.newMedia(fullpath);
                    playlist.appendItem(media);
                    axWindowsMediaPlayer2.currentPlaylist = playlist;
                    axWindowsMediaPlayer2.Ctlcontrols.play();
                    axWindowsMediaPlayer2.playlistCollection.remove(playlist);
                }
                //axWindowsMediaPlayer2.playlistCollection.remove()
            }
*/